/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.persistence.Result;
import com.thinkgem.jeesite.common.utils.I18nUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentType;
import com.thinkgem.jeesite.modules.equipment.entity.EleMonitorParam;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentTypeService;
import com.thinkgem.jeesite.modules.equipment.service.EleMonitorParamService;
import com.thinkgem.jeesite.modules.source.entity.EleSourceGroup;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.servlet.support.RequestContextUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * 设备类型管理Controller
 * @author ZC
 * @version 2017-04-25
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipmentType")
public class EleEquipmentTypeController extends BaseController {
	@Autowired
	private EleMonitorParamService eleMonitorParamService;
	@Autowired
	private EleEquipmentTypeService eleEquipmentTypeService;
	
	@ModelAttribute
	public EleEquipmentType get(@RequestParam(required=false) String id) {
		EleEquipmentType entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentTypeService.get(id);
		}
		if (entity == null){
			entity = new EleEquipmentType();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleEquipmentType:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipmentType eleEquipmentType, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<EleEquipmentType> page = eleEquipmentTypeService.findPage(new Page<EleEquipmentType>(request, response), eleEquipmentType); 
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		page.setLocale(locale);
		model.addAttribute("page", page);
		model.addAttribute("language", locale.getLanguage());
		if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
			model.addAttribute("langCode", locale.getLanguage()+"-cn");
		}else{
			model.addAttribute("langCode", locale.getLanguage());
		}
		return "modules/equipment/eleEquipmentTypeList";
	}

	@RequiresPermissions("equipment:eleEquipmentType:view")
	@RequestMapping(value = "form")
	public String form(EleEquipmentType eleEquipmentType, Model model,HttpServletRequest request) {
		model.addAttribute("eleEquipmentType", eleEquipmentType);
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		model.addAttribute("language", locale.getLanguage());
		if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
			model.addAttribute("langCode", locale.getLanguage()+"-cn");
		}else{
			model.addAttribute("langCode", locale.getLanguage());
		}
		return "modules/equipment/eleEquipmentTypeForm";
	}

	@RequiresPermissions("equipment:eleEquipmentType:edit")
	@RequestMapping(value = "save")
	public String save(EleEquipmentType eleEquipmentType, Model model,HttpServletRequest request,  RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleEquipmentType)){
			return form(eleEquipmentType, model,request);
		}
		User thisuser = UserUtils.getUser();
		eleEquipmentTypeService.save(eleEquipmentType);
		List<EleMonitorParam> paramList = eleMonitorParamService.findEleMonitorParamByEqId(eleEquipmentType.getId());
		if (paramList.isEmpty()) {
			List<Dict> params = DictUtils.getDictList("param_list");
			for (Dict param : params) {
				String title = param.getLabel();
				EleMonitorParam cond = new EleMonitorParam();
				cond.setTitle(title);
				cond.setEquipmentId(eleEquipmentType.getId());
				cond.setCreateDate(new Date(System.currentTimeMillis()));
				cond.setCreateBy(thisuser);
				cond.setSort(param.getSort());
				cond.setIsUsed("1");
				eleMonitorParamService.save(cond);
			}
		} else {

		}
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存设备类型成功";
		if(Global.US.equals(locale)){
			msg = "Save device type success";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentType/?repage";
	}
	
	@RequiresPermissions("equipment:eleEquipmentType:edit")
	@RequestMapping(value = "delete")
	public String delete(EleEquipmentType eleEquipmentType, RedirectAttributes redirectAttributes) {
		String typeId = eleEquipmentType.getId();
		System.out.println(typeId);
		if(eleEquipmentTypeService.findEqipmentsForTheType(typeId) > 0){
			String locale = I18nUtils.getLocaleStr();
			String msg = "此设备类型在使用中";
			if(Global.US.equals(locale)){
				msg = "This type of device is in use";
			}
			addMessage(redirectAttributes, msg);
			return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentType/?repage";
		}
		eleEquipmentTypeService.delete(eleEquipmentType);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除设备类型成功";
		if(Global.US.equals(locale)){
			msg = "Delete device type success";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentType/?repage";
	}

	@RequiresUser
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(HttpServletRequest request,HttpServletResponse response) {
		response.setContentType("application/json; charset=UTF-8");
		List<Map<String, Object>> mapList = Lists.newArrayList();
		Locale locale = RequestContextUtils.getLocaleResolver(request).resolveLocale(request);  
		List<EleEquipmentType> list = eleEquipmentTypeService.findList(new EleEquipmentType());
		for(EleEquipmentType e : list){
			Map<String,Object> map2 = Maps.newHashMap();
			map2.put("id", e.getId());
			map2.put("pId", 0);
			map2.put("name", e.getName());
			map2.put("width", e.getWidth());
			map2.put("height", e.getHeight());
			map2.put("orien", e.getOrientation());
			if(locale.getLanguage().equals(Locale.CHINESE.getLanguage())){
				map2.put("orientation", DictUtils.getDictLabel(e.getOrientation(), "orientation", "竖屏"));
			}
			else{
				map2.put("orientation", DictUtils.getEnDictLabel(e.getOrientation(), "orientation", "Portrait"));
			}
			mapList.add(map2);
		}
		return mapList;
	}
	@RequiresPermissions("equipment:eleEquipmentType:edit")
	@RequestMapping("checkEquipmentType")
	@ResponseBody
	public Result checkEquipmentType(String typeList, String flag) {
		String types = typeList.substring(0, typeList.length() - 1);
		System.out.println(types);
		String status = EleSourceGroup.DEL_FLAG_NORMAL;
		String locale = I18nUtils.getLocaleStr();
		String msg = "审核通过";
		if(Global.US.equals(locale)){
			msg = "passed";
		}
		if ("delete".equals(flag)) {
			status = EleSourceGroup.DEL_FLAG_DELETE;
			msg = "删除成功";
		}
		if ("reject".equals(flag)) {
			status = EleSourceGroup.DEL_FLAG_AUDIT_NO;
			locale = I18nUtils.getLocaleStr();
			msg = "未通过审核";
			if(Global.US.equals(locale)){
				msg = "Not passed";
			}
		}
		for(String typeId:types.split(",")){
			String type = typeId.replaceAll("'", "");
			if(eleEquipmentTypeService.findEqipmentsForTheType(type) > 0){
				System.out.println("11111111111111111111");
				return new Result(Result.FAILURE, "有设备类型在使用中", null);
			}
		}
		User thisuser = UserUtils.getUser();
		eleEquipmentTypeService.batchUpdateEleEquipmentTypeStatus(types, status, thisuser.getId());
		return new Result(Result.SUCCESS, msg, null);
	}
	@RequiresPermissions("equipment:eleEquipmentType:edit")
	@RequestMapping("resubmit")
	@ResponseBody
	public Result resubmitEquipmentType(String typeList, HttpServletRequest request, HttpServletResponse response, Model model) {
		String types = typeList.substring(0, typeList.length() - 1);
		String status = EleSourceGroup.DEL_FLAG_AUDIT;
		String msg = "已重新提交审核";
		System.out.println(types);
		User thisuser = UserUtils.getUser();
		eleEquipmentTypeService.batchUpdateEleEquipmentTypeStatus(types, status, thisuser.getId());
		return new Result(Result.SUCCESS, msg, null);
	}
}